function processFile(blob, fileName) { var text = (($('.extra-input input').first().val() || 'CONFIDENTIAL') + '').trim(); if (!text) { alert('Please enter watermark text.'); return; } loadScriptPromise('https://cdn.jsdelivr.net/npm/pdf-lib@1.17.1/dist/pdf-lib.min.js').then(async function() { try { var srcBytes = await blob.arrayBuffer(); var pdfDoc = await PDFLib.PDFDocument.load(srcBytes, { ignoreEncryption: true }); var font = await pdfDoc.embedFont(PDFLib.StandardFonts.HelveticaBold); var pages = pdfDoc.getPages(); for (var i = 0; i < pages.length; i++) { var page = pages[i]; var w = page.getWidth(); var h = page.getHeight(); var diag = Math.sqrt(w * w + h * h); var size = Math.max(22, Math.min(96, Math.round(diag / 12))); var textWidth = font.widthOfTextAtSize(text, size); var x = (w - textWidth) / 2; var y = (h - size) / 2; page.drawText(text, { x: x, y: y, size: size, font: font, color: PDFLib.rgb(0.55, 0.55, 0.55), rotate: PDFLib.degrees(45), opacity: 0.17 }); } var outBytes = await pdfDoc.save({ useObjectStreams: true }); var base = (fileName || 'document').replace(/\.pdf$/i, ''); add_file_output(URL.createObjectURL(new Blob([outBytes], { type: 'application/pdf' })), base + '-watermarked.pdf'); } catch (err) { alert('Could not watermark this PDF. Encrypted PDFs are not supported.'); } }).catch(function() { alert('Could not load PDF tools in your browser.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }